package com.moon.back.util;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

public class ZipUtils {

    /**
     * 压缩文件夹到 ZIP 文件
     * @param folderPath 要压缩的文件夹路径（如 "/data/files"）
     * @param zipOutputStream ZIP 输出流
     */
    public static void zipFolder(String folderPath, ZipOutputStream zipOutputStream) throws IOException {
        File folder = new File(folderPath);
        if (!folder.exists() || !folder.isDirectory()) {
            throw new FileNotFoundException("文件夹不存在: " + folderPath);
        }

        for (File file : folder.listFiles()) {
            if (file.isDirectory()) {
                // 递归处理子文件夹
                zipFolder(file.getAbsolutePath(), zipOutputStream);
            } else {
                // 写入文件到 ZIP
                ZipEntry zipEntry = new ZipEntry(file.getName());
                zipOutputStream.putNextEntry(zipEntry);
                try (FileInputStream fis = new FileInputStream(file)) {
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = fis.read(buffer)) > 0) {
                        zipOutputStream.write(buffer, 0, len);
                    }
                }
                zipOutputStream.closeEntry();
            }
        }

    }
}